package com.linkhub.oms.data.sync.service.impl;

import com.linkhub.oms.dao.mpservice.IBillListService;
import com.linkhub.oms.dao.mpservice.IUserAccountFlowService;
import com.linkhub.oms.dao.mpservice.IUserService;
import com.linkhub.oms.data.sync.service.*;
import com.linkhub.oms.entity.BillListEntity;
import com.linkhub.oms.entity.UserAccountFlowEntity;
import com.linkhub.oms.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: BillListGenerateBizServiceImpl
 * @Author: Jaycle
 * @Date: 2024/6/30 10:02
 * @Version: 1.0
 */
@Slf4j
@Service
public class BillListGenerateBizServiceImpl implements BillListGenerateBizService {

    @Resource
    private IUserService userService;

    @Resource
    private IUserAccountFlowService userAccountFlowService;

    @Resource
    private IBillListService billListService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void generateBillList() {
        // Todo：检查资金流水统计是否根据创建日期来，是否需要落库每月各项资金流水明细
        List<UserEntity> userEntities = userService.listInWhiteListMerchants();
        if (userEntities != null) {
            // 获取当前日期和时间
            LocalDateTime now = LocalDateTime.now();

            // 获取本月5号的日期和时间
            LocalDateTime thisMonth5th = now.withDayOfMonth(5).withHour(0).withMinute(0).withSecond(0).withNano(0);

            // 未到5号不允许生成账单
            if (now.isBefore(thisMonth5th)) {
                return;
            }

            // 获取上个月5号的日期和时间
            LocalDateTime lastMonth5th = now.minusMonths(1).withDayOfMonth(5).withHour(0).withMinute(0).withSecond(0).withNano(0);

            // 格式化输出
            DateTimeFormatter formatterSeconds = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            DateTimeFormatter formatterMonth = DateTimeFormatter.ofPattern("yyyyMM");
            DateTimeFormatter formatterDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");

            String startDate = lastMonth5th.format(formatterSeconds);
            String endDate = thisMonth5th.format(formatterSeconds);

            String invoiceId = lastMonth5th.format(formatterMonth);
            List<BillListEntity> billListEntities = new ArrayList<>();
            for (UserEntity userEntity : userEntities) {
                BillListEntity billListEntity = new BillListEntity();
                billListEntity.setMerchantUserId(userEntity.getUserId());
                billListEntity.setInvoiceId(invoiceId);
                billListEntity.setInvoiceDate(now.format(formatterDate));
                billListEntity.setStartDate(lastMonth5th.format(formatterDate));
                billListEntity.setEndDate(thisMonth5th.minusDays(1).format(formatterDate));

                List<UserAccountFlowEntity> userAccountFlowEntities = userAccountFlowService.getExpendFee(userEntity.getUserId(), startDate, endDate);
                billListEntity.setFee(String.valueOf(
                        userAccountFlowEntities.stream()
                                .map(UserAccountFlowEntity::getAmount)
                                .reduce(BigDecimal.ZERO, BigDecimal::add)));

                billListEntities.add(billListEntity);
            }
            billListService.deleteByInvoiceId(invoiceId);
            billListService.saveBatch(billListEntities);
        }
    }
}
